package cn.com.hfuu.service;

import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.com.hfuu.ExcelUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.dao.landMapper;
import cn.com.hfuu.dao.pickMapper;
import cn.com.hfuu.dao.userMapper;
import cn.com.hfuu.dao.warehouseteaxMapper;
import cn.com.hfuu.module.land;
import cn.com.hfuu.module.pick;
import cn.com.hfuu.module.user;
import cn.com.hfuu.module.warehouseteax;

@Service("pickservice")
public class PickService {

	@Autowired
	pickMapper pm;
	
	@Autowired
	landMapper landMapper;
	
	@Autowired
	userMapper userMapper;
	
	@Autowired
	warehouseteaxMapper warehouseteaxMapper;

	public List<pick> selectpage(Integer start, Integer limit, pick p) {
		// TODO Auto-generated method stub
		return pm.selectpage(start, limit, p.getPickId(), p.getPickLandid());
	}

	public List<pick> selectpagetotal(pick p) {
		// TODO Auto-generated method stub
		return pm.selectpagetotal(p.getPickId(), p.getPickLandid());
	}

	public int insert(pick p) {
		// TODO Auto-generated method stub

		return pm.insertSelective(p);
	}

	public pick seletbyid(String id) {
		// TODO Auto-generated method stub
		return pm.selectByPrimaryKey(id);
	}

	public int update(pick p) {
		// TODO Auto-generated method stub
		return pm.updateByPrimaryKeySelective(p);
	}

	public List<pick> selecttotal() {
		// TODO Auto-generated method stub
		return pm.selectall();
	}

	public int delete(String id) {
		// TODO Auto-generated method stub
		return pm.deleteByPrimaryKey(id);
	}

	public void upload(InputStream in, MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		List<List<Object>> listob = ExcelUtil.getBankListByExcel(in,
				file.getOriginalFilename());
		UUIdUtil idu = new UUIdUtil();
		for (int i = 0; i < listob.size(); i++) {
			List<Object> ob = listob.get(i);
			System.out.println(ob.toString());
			pick p=new pick();
			p.setPickId(idu.getUuid());
			p.setPickTeaname(String.valueOf(ob.get(0)).trim());
			land l=landMapper.selectbyname(ob.get(1).toString().trim());
			if(l==null){
				throw new RuntimeException("第"+(i+1)+"行茶园名不存在");
			}
			p.setPickLandid(l.getLandId());
			p.setPickQuality(Float.parseFloat(String.valueOf(ob.get(2)).trim()));
			String type=String.valueOf(ob.get(3)).trim();
			if(type.equals("折采")){
			 	p.setPickOperate(0);
			}else if(type.equals("提手采 ")){
				p.setPickOperate(1);
			}else if(type.equals("双手采")){
				p.setPickOperate(2);
			}else if(type.equals("其他")){
				p.setPickOperate(3);
			}else{
				throw new RuntimeException("第"+(i+1)+"行采摘方式不存在");
			}
			
			user u=userMapper.selectByUsername(String.valueOf(ob.get(4)).trim());
			if(u==null){
				throw new RuntimeException("第"+(i+1)+"行采摘人不存在");
			}
			p.setPickUserid(u.getUserId());
			p.setPickTime(String.valueOf(ob.get(5)).trim());
			pm.insertSelective(p);
			warehouseteax teax = new warehouseteax();
			teax.setWarehouseTeaxid(idu.getUuid());
			teax.setWarehousePickid(p.getPickId());
			teax.setWarehouseTeaname(p.getPickTeaname());
			teax.setWarehouseTeaquality(p.getPickQuality());
			teax.setState(0);
			warehouseteaxMapper.insertSelective(teax);
		}
	}

}
